﻿declare var mvcModel: any;

module WorkBasket.Design {
    var SOC = WorkBasket.SOC;
    WorkbasketApp.controller('wbdsDesignCTRL',
        function ($scope) {
            var SOC = WorkBasket.SOC;
            $scope.Units = mvcModel;
            $scope.SelectedGroup = null;
            $scope.SelectedGroupPropertys = [];
            $scope.SelectedUnit = null;
            $scope.SelectedUnitGroups = null;
            $scope.SelectedDomains = [];
            $scope.SelectedGroupMemebers = [];
            $scope.NotFoundMember = null;


            $scope.AddUnit = function () {

                var bu = new WorkBasket.Entities.Unit(0,"My new Business Unit",false,false,[]);
                var obj = SOC.Units().SaveData(bu);
                $scope.Units.pop(obj);


            }
 
            $scope.SelectUnit = function (unit) {
                $scope.SelectedUnit = unit;
                $scope.SelectedUnitGroups = SOC.Groups().GetByParentKey(unit.UnitID);
                SOC.DomainUnits().GetData("GetByUnitID",
                    function (Doms) {
                        $scope.SelectedDomains = Doms;

                    }, unit.UnitID);

             

                

            }

            $scope.SelectGroup = function (Group) {
                $scope.SelectedGroup = Group;
                $scope.SelectedGroupPropertys = SOC.GroupPropertys().GetByParentKey(Group.GroupID);
                $scope.SelectedGroupMembers = SOC.GroupMembers().GetData("GetGroupWBUsers",
                    function (Users) {
                        $scope.SelectedGroupMemebers = Users;

                    }, Group.GroupID);
            }
            $scope.DeleteMember = function (GroupMember) {

                //You have to get the gm because you are not being passed a gm.  Your being passed a WBUser but the sproc p
                //puts GroupMemberID on it from the join.
                var gm:any = SOC.GroupMembers().GetByKey(GroupMember.GroupMemberID);
                SOC.GroupMembers().Delete(gm, gm.GroupMemberID);
                $scope.SelectedGroupMemebers.splice($scope.SelectedGroupMemebers.indexOf(GroupMember), 1);

            }

            $scope.AddGroup = function() {
                var g = new WorkBasket.Entities.Group(0, "My New Group", $scope.SelectedUnit.UnitID);
                var obj = SOC.Groups().SaveData(g);
                $scope.SelectedUnitGroups.push(obj);
             }

            $scope.AddGroupProperty = function() {
                var gp = new WorkBasket.Entities.GroupProperty(0, $scope.SelectedGroup.GroupID, "My new Property", null);
                var obj = SOC.GroupPropertys().SaveData(gp);
                $scope.SelectedGroupPropertys.push(obj);
 

            }
            $scope.UpdateGroup = function (group) {
                SOC.Groups().SaveData(group);
            }
            $scope.UpdateProperty = function (groupProperty) {
                SOC.GroupPropertys().SaveData(groupProperty);
            }
            $scope.DeleteProperty = function (GroupProperty) {
                SOC.GroupPropertys().Delete(GroupProperty, GroupProperty.GroupPropertyID);
                $scope.SelectedGroupPropertys.splice($scope.SelectedGroupPropertys.indexOf(GroupProperty), 1);
            }
            $scope.DeleteGroup = function (group) {
                SOC.Groups().Delete(group, group.GroupID);
                $scope.SelectedUnitGroups.splice($scope.SelectedUnitGroups.indexOf(group), 1);
            }
            $scope.AddDomain = function (Unit) {
 
                var Domain = {DomainID:"0",DomainName:""};
                $scope.SelectedDomains.push(Domain);
               
            }
            $scope.RemoveDomain = function (Unit,Domain) {
                Unit.Domains.splice(Unit.Domains.indexOf(Domain), 1);
            }
            $scope.ValidateDomain = function (Domain) {
                

                SOC.DomainUnits().GetData("ValidateDomain",
                    function (dom) {
                        if (dom != "") {
                            Domain.DomainID = dom.DomainID;
                            Domain.IsValid = true;
                            var du = new WorkBasket.Entities.DomainUnit(0, dom.DomainID, $scope.SelectedUnit.UnitID);
                            SOC.DomainUnits().SaveData(du);
                        } else {
                            Domain.IsValid = false;
                        }


                    },undefined,Domain,"POST");

                

                                                    
            }
            $scope.SaveUnitProperties = function (unit) {
                var obj = SOC.Units().SaveData(unit);
            }
            $scope.Search = function () {


                var srch = { email: "", name: "" }
                srch.email = $scope.SearchEmail;
                srch.name = $scope.SearchName;

                SOC.GroupMembers().GetData("Search",
                    function (results) {
                        $scope.SearchResults = results;
                        if (results == null || results.length == 0) {
                            if (srch.email.length > 0) {
                                $scope.NotFoundMember = srch.email;
                            }
                        }

                    }, undefined, srch, "POST");

            }

            $scope.SendInvite = function (User) {
                var invite = new WorkBasket.Entities.Invite(0,
                                                            User.email,
                                                            new Date().toJSON(),
                                                            false,
                                                            false,
                                                            User.user_id,
                                                            null,
                                                            null,
                                                            $scope.SelectedGroup.GroupID,
                                                            null);
                SOC.Invites().SaveData(invite);
                $scope.SearchResults.splice($scope.SearchResults.indexOf(User), 1);

            }
            $scope.SendBlindInvite = function (email) {
                var invite = new WorkBasket.Entities.Invite(0,
                    email,
                    new Date().toJSON(),
                    false,
                    false,
                    null,
                    null,
                    null,
                    $scope.SelectedGroup.GroupID,
                    null);
                SOC.Invites().SaveData(invite);
                $scope.NotFoundMember = null;

            }


        })
 }